package edu.uclm.esi.tysweb.ajedrez.actions;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.uclm.esi.tysweb.ajedrez.dominio.GestorAjedrez;
import edu.uclm.esi.tysweb.ajedrez.dominio.Jugador;

public class Desconectar extends ActionSupport {
	private SQLException exception;
	
	public String execute() {
		try {
			HttpSession session = ServletActionContext.getRequest().getSession();

			Connection bd=(Connection) session.getAttribute("bd");
			bd.close();
			String email=((Jugador) session.getAttribute("jugador")).getEmail();
			GestorAjedrez gestor=GestorAjedrez.get();
			gestor.cerrarSesion(email);
			session.invalidate();
			return SUCCESS;
		} catch (SQLException e) {
			ActionContext.getContext().getSession().put("exception", e);
			this.exception=e;
			return ERROR;
		}
	}

	public String getResultado() {
		if (exception!=null)
			return exception.toString();
		return "OK";
	}

}
